package com.fcl.oschina.ui.activity;

import android.content.Intent;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.fcl.mylibrary.reflect.OnClick;
import com.fcl.mylibrary.reflect.ViewInject;
import com.fcl.mylibrary.ui.layout.SceneLayout;
import com.fcl.mylibrary.util.FLog;
import com.fcl.mylibrary.webview.MyWebView;
import com.fcl.oschina.Constant;
import com.fcl.oschina.R;
import com.fcl.oschina.adapter.CommentAdapter;
import com.fcl.oschina.adapter.RecommendAdapter;
import com.fcl.oschina.entity.AboutEntity;
import com.fcl.oschina.entity.CommentEntity;
import com.fcl.oschina.entity.DetailResult;
import com.fcl.oschina.model.CommentModel;
import com.fcl.oschina.model.DetailModel;
import com.fcl.oschina.ui.fragment.CommentView;

import java.util.ArrayList;
import java.util.List;

public class DetailActivity extends BaseBackActivity implements DetailView, CommentView {

    private String TAG = "DetailActivity";

    @ViewInject(R.id.body_web)
    MyWebView mBodyWeb;

    @ViewInject(R.id.recommend_recycler)
    RecyclerView mRecommendRecycler;

    @ViewInject(R.id.recommend_layout)
    LinearLayout mRecommendLayout;

    @ViewInject(R.id.comment_layout)
    LinearLayout mCommentLayout;

    @ViewInject(R.id.comment_recycler)
    RecyclerView mCommentRecycler;

    @ViewInject(R.id.more_comment)
    TextView mMoreTv;

    @ViewInject(R.id.scene_layout)
    SceneLayout mSceneLayout;

    @OnClick({R.id.more_comment})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.more_comment:
                Intent intent = new Intent(DetailActivity.this, CommentListActivity.class);
                intent.putExtra("id", mDetail.getId());
                intent.putExtra("type", getTypeNum());
                startActivity(intent);
                break;
        }
    }

    protected int mId;
    protected String mType;
    protected DetailModel mDetailModel;
    protected List<AboutEntity> mRecommendDatas = new ArrayList<>();
    protected RecommendAdapter mRecommendAdapter;
    protected List<CommentEntity> mCommentDatas = new ArrayList<>();
    protected CommentAdapter mCommentAdapter;
    protected DetailResult mDetail;
    protected CommentModel mCommentModel;

    @Override
    protected void init() {
        super.init();
        mToolbar.setTitle(getTitleInt());
        Intent intent = getIntent();
        mId = intent.getIntExtra("id", 0);
        mType = getType();
        FLog.error(TAG, "id:"+mId);

        mDetailModel = new DetailModel(mType, mId, this);
        mSceneLayout.setLoading();
        mDetailModel.getDetail();

        mCommentModel = new CommentModel(this);

        //推荐列表
        mRecommendAdapter = new RecommendAdapter(this, R.layout.item_recommend, mRecommendDatas);
        mRecommendAdapter.setShowFooter(false);
        mRecommendRecycler.setAdapter(mRecommendAdapter);
        mRecommendRecycler.setLayoutManager(new LinearLayoutManager(this));
        mRecommendRecycler.setNestedScrollingEnabled(false);

        //评论列表
        mCommentAdapter = new CommentAdapter(this,R.layout.item_comment, mCommentDatas);
        mCommentAdapter.setShowFooter(false);
        mCommentRecycler.setAdapter(mCommentAdapter);
        mCommentRecycler.setLayoutManager(new LinearLayoutManager(this));
        mCommentRecycler.setNestedScrollingEnabled(false);
    }

    protected int getTitleInt() {
        return R.string.news_detail;
    }

    @Override
    public void setDetail(DetailResult detail) {
        if (detail == null) {
            return;
        }
        mDetail = detail;
        mSceneLayout.setNormal();
        mRecommendDatas = detail.getAbouts();
        if (mRecommendDatas == null || mRecommendDatas.size() == 0) {
            mRecommendLayout.setVisibility(View.GONE);
        } else {
            mRecommendAdapter.setDatas(mRecommendDatas);
        }
        mCommentModel.getCommentList(detail.getId()+"", getTypeNum(), "refer,reply");
        mBodyWeb.loadData(detail.getBody());
    }

    protected String getType() {
        return "news";
    }

    protected int getTypeNum() {
        return Constant.COMMENT_NEWS;
    }

    @Override
    public void setComment(List<CommentEntity> datas, boolean add) {
        if (datas == null || datas.size() == 0) {
            mCommentLayout.setVisibility(View.GONE);
        } else {
            if (add) {
                mCommentAdapter.addDatas(datas);
            } else {
                mCommentAdapter.setDatas(datas);
            }
            if (datas.size() >= 20) {
                mMoreTv.setVisibility(View.VISIBLE);
            }
        }
    }

    @Override
    public void noMoreData() {
        mCommentLayout.setVisibility(View.GONE);
    }
}
